package com.create404.suishoupai.controller;


import cn.jpush.api.push.PushResult;
import com.create404.suishoupai.entity.ChatRecord;
import com.create404.suishoupai.service.IChatRecordService;
import com.create404.suishoupai.utils.AesUtil;
import com.create404.suishoupai.utils.Result;
import com.create404.suishoupai.utils.SendUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "聊天记录接口")
@RestController
@RequestMapping("/chat-record")
public class ChatRecordController {

    @Autowired
    IChatRecordService chatRecordService;

    @GetMapping("/queryRecords")
    public Result queryRecords(String me, String other) {
        int aMe = Integer.parseInt(AesUtil.aesDecrypt(me));
        int aOther = Integer.parseInt(AesUtil.aesDecrypt(other));
        List<ChatRecord> chatRecords = chatRecordService.queryRecords(aMe, aOther);
        return Result.ok(chatRecords);
    }

    @PostMapping("/saveOne")
    public Result saveone(@RequestBody ChatRecord chatRecord) {
        boolean result = chatRecordService.save(chatRecord);
        SendUtils.testSendPush(chatRecord);
        if (result) {
            return Result.ok("发送成功");
        } else {
            return Result.error("发送失败");
        }
    }

    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestBody ChatRecord record) {
        PushResult result = SendUtils.testSendPush(record);
        return Result.ok("保存成功");
    }

    @GetMapping("/getUnreadNum")
    public Result getUnreadNum(String me, String other) {
        int aMe = Integer.parseInt(AesUtil.aesDecrypt(me));
        int aOther = Integer.parseInt(AesUtil.aesDecrypt(other));
        int unreadNum = chatRecordService.getUnreadNum(aMe, aOther);
        return Result.ok(unreadNum);
    }

    @GetMapping("/updateIsread")
    public Result updateIsread(String me, String other) {
        int aMe = Integer.parseInt(AesUtil.aesDecrypt(me));
        int aOther = Integer.parseInt(AesUtil.aesDecrypt(other));
        chatRecordService.updateIsread(aMe, aOther);
        return Result.ok("修改成功");
    }

    @GetMapping("/updateSystemIsread")
    public Result updateIsread(int recordId) {
        chatRecordService.updateSystemIsread(recordId);
        return Result.ok("修改成功");
    }
}
